package com.xtx.service.impl;

import com.xtx.context.BaseContext;
import com.xtx.dto.AddAddressDTO;
import com.xtx.entity.Address;
import com.xtx.entity.Province;
import com.xtx.mapper.AddressMapper;
import com.xtx.service.AddressService;
import com.xtx.vo.AddressVo;
import com.xtx.vo.CountyVo;
import com.xtx.vo.MarketVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /*
    * 获取省列表
     */
    @Override
    public List<Province> getProvinceList() {
        return addressMapper.getProvinceList();
    }
    /*
    * 获取市列表
     */
    @Override
    public List<MarketVo> getMarketList(Integer id) {
        return addressMapper.getMarketList(id);
    }
    /*
    * 获取县区列表
     */
    @Override
    public List<CountyVo> getCountyList(Integer id) {
        return addressMapper.getCountyList(id);
    }
    /*
    * 添加地址
     */
    @Override
    public void addAddress(AddAddressDTO addAddressDTO) {
        //获取省市区县名称
        String province = addressMapper.getProvinceById(addAddressDTO.getProvince());
        String market = addressMapper.getMarketById(addAddressDTO.getMarket());
        String county = addressMapper.getCountyById(addAddressDTO.getCounty());
        String detailedAddress = province + market + county + addAddressDTO.getDetailedAddress();
        //通过链式调用将数据封装成Address对象
        Address address = Address.builder()
                .address(detailedAddress)
                .name(addAddressDTO.getUsername())
                .phone(addAddressDTO.getPhone())
                .userId(BaseContext.getCurrentId())
                .build();
        //操作mapper层插入地址数据
        addressMapper.addAddress(address);
    }
    /*
    * 获取地址列表
     */
    @Override
    public List<AddressVo> getAddressList(Integer id) {
        return addressMapper.getAddressList(id);
    }
}
